package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import database.DBHandler;
import forum.Member;

/**
 * Servlet implementation class viewFriends
 */
public class viewFriends extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public viewFriends() {
        super();
    }
    
    @Override
    public void init(){
    	try {
			super.init();
		} catch (ServletException e) {
			System.err.println("Error in initializing register servlet!!!");
			e.printStackTrace();
		}
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();		
		//DatabaseInterface db = new DBHandler();
		session.setAttribute("forumID", null);
		session.setAttribute("threadID", null);
		String username = (String) session.getAttribute("username");
		if (username != null){
			if ((session.getAttribute("isLoggedIn") != null) && (Boolean)session.getAttribute("isLoggedIn")){
				List<Member> friends = DBHandler.getInstance().getFriends(session);
				List<String> friendsNames = new ArrayList<String>();
				for (int i = 0; i < friends.size(); i++){
					friendsNames.add(friends.get(i).getUsername());
				}
				request.setAttribute("friends", friendsNames);
				RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/friendsList.jsp");		
				dispatcher.forward(request, response);
			}
			else {
				RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/Error.jsp");
				request.setAttribute("errorMsg", "The user "+username+" is not logged in!");
				request.setAttribute("errorLink", "http://" + request.getServerName() + ":"
						+ request.getServerPort() + request.getContextPath());
				dispatcher.forward(request, response);
			}
		}
		else {
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/Error.jsp");
			request.setAttribute("errorMsg", "There is no user!!!");
			request.setAttribute("errorLink", "http://" + request.getServerName() + ":"
					+ request.getServerPort() + request.getContextPath());
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
	@Override
	public void destroy(){
		super.destroy();
	}

}
